/**
 * 
 */
package org.istt.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.istt.dao.ProcessStatusDao;
import org.istt.domain.ProcessStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

/**
 * This class interacts with Spring and Hibernate to save and
 * retrieve ProcessStatus objects.
 *
 * @author NDTHO
 */
@Repository(value = "processStatusDao")
public class ProcessStatusDaoHibernate extends BaseDaoHibernate<ProcessStatus, Long> implements ProcessStatusDao {
    Log logger = LogFactory.getLog(ProcessStatusDaoHibernate.class);
    
    @Autowired
    public ProcessStatusDaoHibernate(SessionFactory sessionFactory) {
		super(sessionFactory);
	}
    
	@SuppressWarnings("unchecked")
	@Override
	public List<ProcessStatus> searchProcessStatus(final String status) {
		return hibernateTemplate.execute(new HibernateCallback<List<ProcessStatus>>() {
			@Override
			public List<ProcessStatus> doInHibernate(Session session) throws HibernateException,	SQLException {
						Criteria criteria = session.createCriteria(ProcessStatus.class);
						if(status!=null && !"".equals(status)) criteria.add(Restrictions.ilike("status", "%"+status+"%"));
						return criteria.list();
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ProcessStatus> getNextProcessStatus(final Long status) {
		
		return hibernateTemplate.execute(new HibernateCallback<List<ProcessStatus>>() {
			@Override
			public List<ProcessStatus> doInHibernate(Session session) throws HibernateException,	SQLException {
						Criteria criteria = session.createCriteria(ProcessStatus.class);
						if(status!=null && !"".equals(status)) criteria.add(Restrictions.not(Restrictions.eq("id", status)));
						return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProcessStatus> findLstPreviousProcessStatus(final Integer curPosition, final String fileId) {
		
		return hibernateTemplate.execute(new HibernateCallback<List<ProcessStatus>>() {
			@Override
			public List<ProcessStatus> doInHibernate(Session session) throws HibernateException,	SQLException {
				Criteria criteria = session.createCriteria(ProcessStatus.class);
				criteria.setFetchMode("WorkflowProcess", FetchMode.JOIN)
				.setFetchMode("Workflow", FetchMode.JOIN)
				.setFetchMode("Category", FetchMode.JOIN)
				.setFetchMode("File", FetchMode.JOIN)
				;
				criteria.createAlias("workflowProcesses", "t1");
				criteria.createAlias("t1.workflow", "t2");
				criteria.createAlias("t2.categories", "t3");
				criteria.createAlias("t3.files", "t4");				
				if (curPosition != 0) {
					criteria.add(Restrictions.lt("t1.position", curPosition));
				}
				if (fileId != null) {
					criteria.add(Restrictions.eq("t4.id", fileId));
				}				
				return criteria.list();						
						
			}
		});
	}	
}
